﻿/*
 ***************************************************************************
 * Microsoft Visual C# .NET
 * 名称：       App_Search_HealthArchivesMdcSrhForm
 * 类型：       控制层
 * 功能说明：   健康档案查询-药品详细列表页面
 * 作者：       章 昕
 * 创建日期：   2007/09/27
 * 修改日期     修改者      修改内容
 * 
 * 
 * ---
 ***************************************************************************
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Input.Bean;
using log4net;
using Shpm.System;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.App.Commons.Utils;
using Shpm.App.Search.Dao;
using Shpm.System.Validator;
using Shpm.System.Web.Control;

public partial class App_Search_HealthArchivesMdcSrhForm : System.Web.UI.Page
{
    // log
    protected static ILog log = LogManager.GetLogger(typeof(App_Search_HealthArchivesMdcSrhForm));

    // gridview 停止用药列
    private const int COLUMN_STOPFLG = 5;
    // gridview 药方内容摘要列
    private const int COLUMN_MDETAILS = 4;    

    // page load
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            initPageControls();

            // 取得当前登录的医生id信息
            SysUserManager manager = new SysUserManager(Context);
            SysUserBean sysUserBean = manager.getSysUserBean();
            MstSysUserBean mstSysUserBean =
                sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
            MstEmployeeBean mstEmpBean =
                sysUserBean == null ? null : sysUserBean.getEmployeeBean();
            string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;
            string emplyId = mstSysUserBean == null ? "" : mstSysUserBean.EmployeeId;
            string emplyRole = mstEmpBean == null ? "" : mstEmpBean.Role;
            ViewState["DOCTOR_ID"] = emplyId;
            ViewState["MEMBER_NO"] = Convert.ToString(Context.Items["MEMBER_NO"]);

            // 保存接收的参数
            ViewState["InpMemberId"] = Convert.ToString(Context.Items["InpMemberId"]);
            ViewState["InpMemberName"] = Convert.ToString(Context.Items["InpMemberName"]);
            ViewState["InpSex"] = Convert.ToString(Context.Items["InpSex"]);
            ViewState["InpDisease"] = Convert.ToString(Context.Items["InpDisease"]);
         
            ViewState["HidMemberId"] = Convert.ToString(Context.Items["HidMemberId"]);
            ViewState["HidMemberName"] = Convert.ToString(Context.Items["HidMemberName"]);
            ViewState["HidSex"] = Convert.ToString(Context.Items["HidSex"]);
            ViewState["HidDisease"] = Convert.ToString(Context.Items["HidDisease"]);
            ViewState["HidCommand"] = Convert.ToString(Context.Items["HidCommand"]);
            ViewState["HidPageIndex"] = Context.Items["HidPageIndex"];


            if (Context.Items.Contains("ParamPageIndex"))
            {
                ViewState["ParamPageIndex"] = Context.Items["ParamPageIndex"];
            }

            //pop画面控制
            if (Context.Items.Contains("POP"))
            {
                this.content.Attributes.Add("class", "content1");
                this.content.Attributes.Add("style", "width:900px");
                this.divBar.Visible = true;
                this.btnBack.Visible = false;
                this.Page.Title = "会员信息";
            }
        }
    }

    // before render
    void Page_PreRender(object sender, EventArgs e)
    {
        // get current doctor responsibility members list
        HealthArchivesDao dao = new HealthArchivesDao();
        IList lstRst = null;
        HealthMedicineBean beanHM = new HealthMedicineBean();
        beanHM.MemberNo = Convert.ToString(ViewState["MEMBER_NO"]);
        beanHM.MedicineType = Convert.ToString(ViewState["HidMedicineType"]);
        if (!string.IsNullOrEmpty(Convert.ToString(ViewState["HidBeginDate"])))
            beanHM.BeginDate = DateUtils.convertToDate(Convert.ToString(ViewState["HidBeginDate"]));
        if (!string.IsNullOrEmpty(Convert.ToString(ViewState["HidEndDate"])))
            beanHM.EndDate = DateUtils.convertToDate(Convert.ToString(ViewState["HidEndDate"]));        
        if (!string.IsNullOrEmpty(Convert.ToString(ViewState["HidStopMedicine"])))
            beanHM.CancelFlg = Convert.ToBoolean(Convert.ToInt32(ViewState["HidStopMedicine"]));

        lstRst = dao.ListArchivesMedicine(beanHM);
        viewSrhRst.DataSource = lstRst;
        viewSrhRst.DataBind();
        if (lstRst != null && lstRst.Count > 0)
        {
            beanHM = (HealthMedicineBean)lstRst[0];
            lblMemSex.Text = beanHM.MemberSex;
            lblMemId.Text = beanHM.MemberNo;
            lblMemName.Text = beanHM.MemberName;
            lblMemTel.Text = beanHM.MemberTel;
            lblRoomNo.Text = beanHM.ApartmentNo;
            lblDocId.Text = beanHM.DoctorId;
        }
    }

    // page index change
    protected void viewSrhRst_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        // 设置分页编号
        viewSrhRst.PageIndex = e.NewPageIndex;
    }

    // details click
    protected void viewSrhRst_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (Request["x"] == null || Request["y"] == null)
            return;

        string rowId = "";
        string url = "~/App/Input/HealthTherapyDetailSrhForm.aspx";       
        if (e.CommandName == "Details")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            // Get the Key value from the GridView control.
            DataKey key = viewSrhRst.DataKeys[index];
            rowId = Convert.ToString(key.Value);
            url = "~/App/Input/HealthTherapyDetailUpdForm.aspx";
        }

        // 传递分页编号
        Context.Items["ROW_ID"] = rowId;
        Context.Items["CURE_ID"] = ViewState["CURE_ID"];
        Context.Items["ParamPageIndex"] = viewSrhRst.PageIndex;
        //Response.Redirect("~/App/Input/HealthTherapyUpdForm.aspx?CURE_ID=" + cureId);
        ControllerUtils.Transfer(Context, url, true);

    }

    // adjust bind data
    protected void viewSrhRst_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {            
            // reset bind data
            if (((HealthMedicineBean)e.Row.DataItem).MemberMedicineId == null)
            {
                e.Row.Visible = false;
            }
            else 
            {
                // 是否停药
                e.Row.Cells[COLUMN_STOPFLG].Text = "正常用药";
                if (Convert.ToBoolean(((HealthMedicineBean)e.Row.DataItem).CancelFlg))
                {
                    e.Row.Cells[COLUMN_STOPFLG].ForeColor = System.Drawing.Color.Red;
                    e.Row.Cells[COLUMN_STOPFLG].Text = "已停药";
                }
                // client click 画面详细用药
                ImageButton imgBtn = ((ImageButton)e.Row.FindControl("ImageButton1"));
                imgBtn.Attributes.Add("onclick", "return openDetail('" + ((HealthMedicineBean)e.Row.DataItem).MemberMedicineId + "');");

                string mDetailText = "";
                HealthArchivesDao dao = new HealthArchivesDao();
                HealthMedicineDetailBean beanHMD = new HealthMedicineDetailBean();
                beanHMD.MemberMedicineId = ((HealthMedicineBean)e.Row.DataItem).MemberMedicineId;
                IList listMDetail = null;
                listMDetail = dao.ListArchivesMedicineDetail(beanHMD);
                IEnumerator itr = listMDetail.GetEnumerator();
                while (itr.MoveNext())
                {
                    beanHMD = (HealthMedicineDetailBean)itr.Current;
                    if (!string.IsNullOrEmpty(beanHMD.MedicineName)
                        && beanHMD.MedicineName.Replace(" ","").Length > 0)
                    mDetailText += beanHMD.MedicineName + "，";
                }
                if (mDetailText.Length > 0)
                    e.Row.Cells[COLUMN_MDETAILS].Text = mDetailText.Substring(0,mDetailText.Length - 1);
            }
        }
    }

    // 检索按钮
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        ComValidateChecker vd = new ComValidateChecker();       
        vd.Add(new CheckDate(txtBeginDate.Text, new ComMessage("E0037", "开始日期")));
        vd.Add(new CheckDate(txtEndDate.Text, new ComMessage("E0037", "结束日期")));
        vd.Add(new CheckDateRange(txtBeginDate.Text, txtEndDate.Text, new ComMessage("E0329", new string[] { "开始日期", "结束日期" })));
        if (!vd.DoValidator())
        {
            ((Design_MasterPage)this.Master).InfoMessage = vd.ErrorMessage;
            return;
        }

        ViewState["HidBeginDate"] = txtBeginDate.Text;
        ViewState["HidEndDate"] = txtEndDate.Text;
        ViewState["HidMedicineType"] = lstMedicineType.SelectedValue;
        ViewState["HidStopMedicine"] = lstMdcStop.SelectedValue;
        viewSrhRst.PageIndex = 0;
    }

    // 返回按钮
    protected void btnBack_Click(object sender, EventArgs e)
    {
        string url = "~/App/Search/HealthArchivesSrhForm.aspx";

        // 回传接收的参数
        Context.Items["InpMemberId"] = ViewState["InpMemberId"];
        Context.Items["InpMemberName"] = ViewState["InpMemberName"];
        Context.Items["InpSex"] = ViewState["InpSex"];
        Context.Items["InpDisease"] = ViewState["InpDisease"];       
        
        Context.Items["HidMemberId"] = ViewState["HidMemberId"];
        Context.Items["HidMemberName"] = ViewState["HidMemberName"];
        Context.Items["HidSex"] = ViewState["HidSex"];
        Context.Items["HidDisease"] = ViewState["HidDisease"];
        Context.Items["HidCommand"] = ViewState["HidCommand"];
        Context.Items["HidPageIndex"] = ViewState["HidPageIndex"];
 
        ControllerUtils.Transfer(Context, url, true);    
    }

    // bind common catalog data 
    private void initPageControls()
    {
        HealthArchivesDao dao = new HealthArchivesDao();
        CommonDictionaryBean beanComDict = new CommonDictionaryBean();
        IList lstCat = null;

        // 用药种类
        log.Fatal("\n ***** get T_MEMBER_MEDICINE.MEDICINE_TYPE catalog *****");
        beanComDict.Catalog = "T_MEMBER_MEDICINE.MEDICINE_TYPE";
        lstCat = dao.FindByCatalog(beanComDict);
        lstMedicineType.Items.Clear();
        lstMedicineType.Items.Add("");
        bindDataToListControl("lstMedicineType", lstCat);

        // 是否停用
        lstMdcStop.Items.Clear();
        lstMdcStop.Items.Add("");
        lstMdcStop.Items.Add(new ListItem("正常用药", "0"));
        lstMdcStop.Items.Add(new ListItem("停药", "1"));
    }

    // bind db data to selected controls
    private void bindDataToListControl(string ControlId, IList LstData)
    {
        if (LstData == null || LstData.Count == 0) return;

        foreach (CommonDictionaryBean bean in LstData)
        {
            if (this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId) is DropDownList)
            {
                ((DropDownList)this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId)).Items.Add(new ListItem(bean.Content, bean.DictNo.ToString()));
            }
            else if ((this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId) is CheckBoxList)
                || (this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId) is RadioButtonList))
            {
                ((ListControl)this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId)).Items.Add(new ListItem(bean.Content, bean.DictNo.ToString()));
            }
        }
    }
}
